﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Net.Mime;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using SubSonic;
using LinFen;

public partial class webUserControl_sysAdd : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
     if(!IsPostBack)
     {
            DropDownList1.Items.Clear();
            DropDownList2.Items.Clear();
            DropDownList3.Items.Clear();
            AddItems(DropDownList1,"sysCatalog","0",0);
            AddItems(DropDownList2,"sysCityName","0",0);
            AddItems(DropDownList3,"sysDangci","0",0);
            Query q = new Query("sysDistrictName");
            q.AddWhere("cityid", DropDownList2.SelectedValue);
            IDataReader dr = q.ExecuteReader();
            while (dr.Read())
            {
                ListItem li = new ListItem();
                li.Value = dr[0].ToString();
                li.Text = dr[1].ToString();
                DropDownList4.Items.Add(li);
            }
     }
          
        
    }
    static void AddItems(DropDownList ddl, string tb, string id, int isType)
    {
        if (isType == 1)
        {
            for (int i = 0; i <= 10; i++)
            {
                ListItem li = new ListItem();
                li.Value = i.ToString();
                li.Text = i.ToString();
                if (id == i.ToString())
                {
                    li.Selected = true;
                }
                ddl.Items.Add(li);
            }
        }
        else
        {
            Query q = new Query(tb);
            IDataReader dr = q.ExecuteReader();
            while (dr.Read())
            {
                ListItem li = new ListItem();
                li.Value = dr[0].ToString();
                li.Text = dr[1].ToString();
                if (id == dr[0].ToString())
                {
                    li.Selected = true;
                }
                ddl.Items.Add(li);
            }
        }

    }

    protected void btnDel1_Click(object sender, EventArgs e)
    {
        
        SysCatalog.Delete("id", DropDownList1.SelectedValue);
        Response.Redirect(Request.Url.ToString());  
       
    }
    protected void btnDel2_Click(object sender, EventArgs e)
    {
        SysCityName.Delete("id", DropDownList2.SelectedValue);
        Response.Redirect(Request.Url.ToString());
    }
    protected void btnDel3_Click(object sender, EventArgs e)
    {
        SysDangCi.Delete("id", DropDownList1.SelectedValue);
        Response.Redirect(Request.Url.ToString());
    }
    protected void btnAdd1_Click(object sender, EventArgs e)
    {
        if (!isExistStr("syscatalog", "classname", TextBox1.Text))
        {
            SysCatalog.Insert(TextBox1.Text);
            Response.Redirect(Request.Url.ToString());
        }
        else
        {
            Literal1.Text = "<script language=javascript>alert('名称有重复！请重新输入')</script>";
        }
        
    }
    protected void btnAdd2_Click(object sender, EventArgs e)
    {

        if (!isExistStr("SysCityName", "cityname", TextBox2.Text))
        {
            SysCityName.Insert(TextBox2.Text);
            Response.Redirect(Request.Url.ToString());
        }
        else
        {
            Literal1.Text = "<script language=javascript>alert('名称有重复！请重新输入')</script>";
        }
        
    }
    protected void btnAdd3_Click(object sender, EventArgs e)
    {
        if (!isExistStr("SysDangCi", "dangciname", TextBox3.Text))
        {
            SysDangCi.Insert(TextBox3.Text);
            Response.Redirect(Request.Url.ToString());
        }
        else
        {
            Literal1.Text = "<script language=javascript>alert('名称有重复！请重新输入')</script>";
        }
        
      
    }
    protected void btnEdit1_Click(object sender, EventArgs e)
    {
        if (!isExistStr("syscatalog", "classname", TextBox1.Text))
        {
            Query q = new Query("sysCatalog");
            q.AddUpdateSetting("className", TextBox1.Text).AddWhere("id", DropDownList1.SelectedValue);
            q.ExecuteReader();
            Response.Redirect(Request.Url.ToString());
        }
        else
        {
            Literal1.Text = "<script language=javascript>alert('名称有重复！请重新输入')</script>";
        }
       
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList4.Items.Clear();
        Query q = new Query("sysDistrictName");
        q.AddWhere("cityid", DropDownList2.SelectedValue);
        IDataReader dr = q.ExecuteReader();
        while (dr.Read())
        {
            ListItem li = new ListItem();
            li.Value = dr[0].ToString();
            li.Text = dr[1].ToString();
            DropDownList4.Items.Add(li);
        }
    }
    public bool isExistStr(string tb,string cl,string value)
    {
        Query q = new Query(tb);
        q.AddWhere(cl, value);
        return q.GetRecordCount()>0;
    }

    protected void btnAdd4_Click(object sender, EventArgs e)
    {
        //if (!isExistStr("SysDistrictName", "DistrictName", TextBox4.Text))
        //{
        //    SysDistrictName.Insert(TextBox4.Text, int.Parse(DropDownList2.SelectedValue));
        //    Response.Redirect(Request.Url.ToString());
        //}
        //else
        //{
        //    Literal1.Text = "<script language=javascript>alert('名称有重复！请重新输入')</script>";
        //}
        Query q = new Query("SysDistrictName");
        q.AddWhere("DistrictName", TextBox4.Text);
        q.AddWhere("cityid", DropDownList2.SelectedValue);
        if(q.GetRecordCount()==0)
        {
            SysDistrictName.Insert(TextBox4.Text, int.Parse(DropDownList2.SelectedValue));
            Response.Redirect(Request.Url.ToString()); 
        }
        else
        {
             Literal1.Text = "<script language=javascript>alert('名称有重复！请重新输入')</script>";
        }
    }
    protected void btnEdit4_Click(object sender, EventArgs e)
    {
        if (!isExistStr("SysDistrictName", "DistrictName", TextBox4.Text))
        {
            Query q = new Query("sysDistrictname");
            q.AddUpdateSetting("districtname", TextBox4.Text).AddWhere("id", DropDownList4.SelectedValue);
            q.ExecuteReader();
            Response.Redirect(Request.Url.ToString());
        }
        else
        {
            Literal1.Text = "<script language=javascript>alert('名称有重复！请重新输入')</script>";
        }
    }
    protected void btnDel4_Click(object sender, EventArgs e)
    {
        SysDistrictName.Delete("id", DropDownList4.SelectedValue);
        Response.Redirect(Request.Url.ToString());
    }
    protected void btnEdit2_Click(object sender, EventArgs e)
    {
        if (!isExistStr("SysCityName", "cityname", TextBox2.Text))
        {
            Query q = new Query("sysCityName");
            q.AddUpdateSetting("cityName", TextBox2.Text).AddWhere("id", DropDownList2.SelectedValue);
            q.ExecuteReader();
            Response.Redirect(Request.Url.ToString());
        }
        else
        {
            Literal1.Text = "<script language=javascript>alert('名称有重复！请重新输入')</script>";
        }
        
    }
    protected void btnEdit3_Click(object sender, EventArgs e)
    {
        if (!isExistStr("SysDangCi", "dangciname", TextBox3.Text))
        {
            Query q = new Query("sysDangci");
            q.AddUpdateSetting("DangciName", TextBox3.Text).AddWhere("id", DropDownList3.SelectedValue);
            q.ExecuteReader();
            Response.Redirect(Request.Url.ToString());
        }
        else
        {
            Literal1.Text = "<script language=javascript>alert('名称有重复！请重新输入')</script>";
        }
    }
}
